package rexsee.xml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import rexsee.core.utilities.Escape;

public class XmlElement {
	public String tagName;
	public String type; //root|tag|text|cdsect|comment|docdecl
	public int depth;
	public XmlElement parent;
	public HashMap<String, String> attributes;
	public String text;
	public ArrayList<XmlElement> childs;
	public XmlElement(XmlElement parent, String tagName, int depth, String type) {
		this.type = type;
		this.depth = depth;
		this.parent = parent;
		this.tagName = tagName;
		this.attributes = null;
		this.childs = null;
		this.text = null;
	}
	public void addAttribute(String name, String value) {
		if (attributes == null) attributes = new HashMap<String, String>();
		attributes.put(name, value);
	}
	public void addChild(XmlElement child) {
		if (childs == null) childs = new ArrayList<XmlElement>();
		child.parent = this;
		childs.add(child);
	}
	@Override
	public String toString() {
		String rtn = "{";
		rtn += "\"tagName\":\"" + tagName + "\"";
		rtn += ",\"type\":\"" + type + "\"";
		rtn += ",\"depth\":" + depth;
		if (attributes != null) {
			String attr = "";
			Set<String> nameSet = attributes.keySet();
			Iterator<String> nameIterator = nameSet.iterator();
			while (nameIterator.hasNext()) {
				if (!attr.equals("")) attr += ",";
				String name = nameIterator.next();
				String value = attributes.get(name);
				attr += "\"" + name + "\":\"" + value + "\"";
			}
			rtn += ",\"attributes\":{" + attr + "}";
		}
		if (text != null) {
			rtn += ",\"text\":\"" + Escape.escape(text) + "\"";
		}
		if (childs != null && childs.size() != 0) {
			rtn += ",\"childs\":" + arrayListToJson(childs);
		}
		rtn += "}";
		return rtn;
	}
	public static String arrayListToJson(ArrayList<XmlElement> list) {
		if (list == null) return "[]";
		String rtn = "";
		for (int i = 0; i < list.size(); i++) {
			if (i > 0) rtn += ",";
			rtn += list.get(i).toString();
		}
		return "[" + rtn + "]";
	}
	public boolean attributeEquals(String attributeName, String attributeValue) {
		return (attributeName != null && attributeValue != null && attributes != null && attributeValue.equals(attributes.get(attributeName.toLowerCase()))) ? true : false;
	}
	public void getChildsByAttribute(ArrayList<XmlElement> results, String attributeName, String attributeValue) {
		if (attributeEquals(attributeName, attributeValue)) results.add(this);
		if (childs == null || childs.size() == 0) return;
		for (int i = 0; i < childs.size(); i++) {
			childs.get(i).getChildsByAttribute(results, attributeName, attributeValue);
		}
	}
	public void getChildsByTagName(ArrayList<XmlElement> results, String tagName) {
		if (tagName.equalsIgnoreCase(this.tagName)) results.add(this);
		if (childs == null || childs.size() == 0) return;
		for (int i = 0; i < childs.size(); i++) {
			childs.get(i).getChildsByTagName(results, tagName);
		}
	}
	public void getChildsByType(ArrayList<XmlElement> results, String type) {
		if (type.equalsIgnoreCase(this.type)) results.add(this);
		if (childs == null || childs.size() == 0) return;
		for (int i = 0; i < childs.size(); i++) {
			childs.get(i).getChildsByType(results, type);
		}
	}
	public void getChildsByDepth(ArrayList<XmlElement> results, int depth) {
		if (depth == this.depth) results.add(this);
		if (childs == null || childs.size() == 0) return;
		for (int i = 0; i < childs.size(); i++) {
			childs.get(i).getChildsByDepth(results, depth);
		}
	}
}
